package io.renren.modules.communityService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import io.renren.modules.communityService.dto.MiniAppDbSuggestionsListDTO;
import io.renren.modules.communityService.dto.MiniAppDbSuggestionsSaveDTO;
import io.renren.modules.communityService.enums.DbSuggestionsStatus;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.communityService.dao.DbSuggestionsDao;
import io.renren.modules.communityService.entity.DbSuggestionsEntity;
import io.renren.modules.communityService.dto.DbSuggestionsDTO;
import io.renren.modules.communityService.vo.DbSuggestionsVO;
import io.renren.modules.communityService.service.DbSuggestionsService;
import io.renren.modules.communityService.conver.DbSuggestionsConver;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;


@Service("dbSuggestionsService")
public class DbSuggestionsServiceImpl extends ServiceImpl<DbSuggestionsDao, DbSuggestionsEntity> implements DbSuggestionsService {

    @Override
    public PageUtils<DbSuggestionsVO> queryPage(DbSuggestionsDTO dbSuggestions) {
        IPage<DbSuggestionsEntity> page = baseMapper.selectPage(
                new Query<DbSuggestionsEntity>(dbSuggestions).getPage(),
                new QueryWrapper<DbSuggestionsEntity>()
        );

        return PageUtils.<DbSuggestionsVO>page(page).setList(DbSuggestionsConver.MAPPER.conver(page.getRecords()));
    }
    @Override
    public DbSuggestionsVO getById(Integer id) {
        return DbSuggestionsConver.MAPPER.conver(baseMapper.selectById(id));
    }

    @Override
    public boolean save(DbSuggestionsDTO dbSuggestions) {
        DbSuggestionsEntity dbSuggestionsEntity = DbSuggestionsConver.MAPPER.converDTO(dbSuggestions);
        return this.save(dbSuggestionsEntity);
    }

    @Override
    public boolean updateById(DbSuggestionsDTO dbSuggestions) {
        DbSuggestionsEntity dbSuggestionsEntity = DbSuggestionsConver.MAPPER.converDTO(dbSuggestions);
        return this.updateById(dbSuggestionsEntity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }

    @Override
    public void save(MiniAppDbSuggestionsSaveDTO dbSuggestions) {

        DbSuggestionsEntity dbSuggestionsEntity = DbSuggestionsConver.MAPPER.conver1(dbSuggestions);
        dbSuggestionsEntity.setImage(JSONUtil.toJsonStr(dbSuggestions.getImages()));
        dbSuggestionsEntity.setStatus(DbSuggestionsStatus.ZERO.getKey());
        dbSuggestionsEntity.setCreatedAt(new Date());
        dbSuggestionsEntity.setUpdatedAt(new Date());
        this.save(dbSuggestionsEntity);
    }

    @Override
    public PageUtils queryPage(MiniAppDbSuggestionsListDTO dto) {
        IPage<DbSuggestionsEntity> page = baseMapper.selectPage(
                new Query<DbSuggestionsEntity>(dto).getPage(),
                new QueryWrapper<DbSuggestionsEntity>().lambda()
                .eq(ObjectUtil.isNotNull(dto.getUserId()),DbSuggestionsEntity::getUserId,dto.getUserId())
        );

        return PageUtils.<DbSuggestionsVO>page(page).setList(DbSuggestionsConver.MAPPER.conver(page.getRecords()));
    }

}
